package com.fafu.mq.mqserver.datacenter;

import com.fafu.mq.MqApplication;
import com.fafu.mq.mqserver.core.Binding;
import com.fafu.mq.mqserver.core.Exchange;
import com.fafu.mq.mqserver.core.ExchangeType;
import com.fafu.mq.mqserver.core.MSGQueue;
import com.fafu.mq.mqserver.mapper.MetaMapper;

import java.io.File;
import java.util.List;

public class DataBaseManger {
//    @Autowired
    private MetaMapper metaMapper;

    public void init(){
        //手动获取metaMapper对象
        metaMapper = MqApplication.context.getBean(MetaMapper.class);
        if(!checkDBExists()){
            //数据库不存在, 进行初始化
            //创建数据库目录
            File dataDir = new File("./data");
            dataDir.mkdirs();
            //创建数据库表
            createTable();
            //插入默认数据
            createDefaultData();
            System.out.println("[DataBaseManger] 数据初始化完成! ");
        }else {
            //数据库已存在
            System.out.println("[DataBaseManger] 数据初始化完成! ");
        }
    }


    private boolean checkDBExists() {
        File file = new File("./data/meta.db");
        if(file.exists()){
            return true;
        }
        return false;
    }

    //建库操作不需要手动执行, Mybatis会自动建库
    private void createTable() {
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        System.out.println("[DataBaseManger] 创建表完成! ");

    }

    //此处添加一个默认的交换机
    //RabbitMQ 中有一个匿名的交换机, 类型是DIRECT
    private void createDefaultData() {
        //构造一个默认的交换机
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        System.out.println("[DataBaseManger] 创建初始数据完成! ");

    }
    public void deleteDB(){
        File file = new File("./data/meta.db");
        boolean ret = file.delete();
        if(ret){
            System.out.println("[DataBaseManger] 删除数据库文件成功! ");
        }else{
            System.out.println("[DataBaseManger] 删除数据库文件失败! ");

        }

        File dataDir = new File("./data");
        //使用 delete 删除目录, 要保证目录里是空的, 没有文件存在
        ret = dataDir.delete();
        if(ret){
            System.out.println("[DataBaseManger] 删除数据库文件夹成功! ");
        }else{
            System.out.println("[DataBaseManger] 删除数据库文件夹失败! ");

        }
    }

    public void insertExchange(Exchange exchange){
        metaMapper.insertExchange(exchange);
    }

    public void deleteExchange(String exchangeName){
        metaMapper.deleteExchange(exchangeName);
    }

    public void insertQueue(MSGQueue queue){
        metaMapper.insertQueue(queue);
    }

    public void deleteQueue(String queueName){
        metaMapper.deleteQueue(queueName);
    }

    public void insertBinding(Binding bindIng){
        metaMapper.insertBinding(bindIng);
    }

    public void deleteBinding(Binding bindIng){
        metaMapper.deleteBinding(bindIng);
    }

    public List<Exchange> selectAllExchanges(){
        return metaMapper.selectAllExchanges();
    }

    public List<MSGQueue> selectAllMSGQueues(){
        return metaMapper.selectAllMSGQueues();
    }

    public List<Binding> selectAllBindings(){
        return metaMapper.selectAllBindings();
    }
}
